package com.topn2;

import org.apache.hadoop.io.WritableComparator;

// 排序比较器
public class TSortComparator extends WritableComparator {

    public TSortComparator() {
        super(Tkey.class, true);
    }

    /**
     * 按照年月、相同的年月按照温度倒序
     * @return
     */
    @Override
    public int compare(Object a, Object b) {
        Tkey k1 = (Tkey) a;
        Tkey k2 = (Tkey) b;
        int yearCompare = Integer.compare(k1.getYear(), k2.getYear());
        if (yearCompare == 0) {
            int monthCompare = Integer.compare(k1.getMonth(), k2.getMonth());
            if (monthCompare == 0) {
                return -Integer.compare(k1.getTemp(), k2.getTemp());
            }
            return monthCompare;
        }
        return yearCompare;
    }
}
